<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Shows (all) article, sortet by order / category 
 * get: article.tpl.php
 * used from article.page.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
$file = 'module/function/article.pagination.func.php';
if (is_file($file))
	include $file;
$functions = 'module/function/article.func.php';
if (is_file($functions))
	include_once $functions;

$result = db::getInstance()->rawQuery("SELECT category_id, category_name, category_active, " .
	"category_order, category_top FROM category " .
	"WHERE category_top = '0' " .
	"ORDER BY category_order");

// Database error
if ($result === NULL)
{
	message::setErrorRow(_('No data could be found! - Contact Admin!'), true);
}
else
{
	$refPage->assign('pageTitel', _('Article table'));
	$refPage->assign('formStart', 'index2.php?page=article&sub=article&func=delete');

	/**
	 *  create list of categorys to filter article
	 */
	$categoryList = array();


	db::getInstance()->orderBy('category_top', 'asc');
	//db::getInstance()->orderBy('category_order', 'asc');
	$cols = Array('category_id', 'category_name', 'category_active');
	$categorys = db::getInstance()->get('category', null, $cols);
	$categorys = sortCategorys($result, true);

	$categoryList[] = array('categoryIdx' => 0,
	 'categoryNamex' => _('Display all'),
	 'categoryCheckedx' => '');


	foreach ($categorys as $cat)
	{
		if (isset($_SESSION['articleFilter']) and $_SESSION['articleFilter'] ==
			$cat['category_id'])
		{
			$categoryList[] = array('categoryIdx' => $cat['category_id'],
			 'categoryNamex' => $cat['category_name'],
			 'categoryCheckedx' => 'selected="selected"');
		}
		else
		{
			$categoryList[] = array('categoryIdx' => $cat['category_id'],
			 'categoryNamex' => $cat['category_name'],
			 'categoryCheckedx' => '');
		}
	}

	$refPage->assign('categoryListx', $categoryList);
	$refPage->assign('categoryList', _('Filter') . ': ');

	$refPage->assign('btnRefresh',
		'<input type="submit" name="submit" id="refresh" value="' .
		_('Refresh Table') . '" />');

	/**
	 * create article-table
	 */
	$productListx = array();
	$products = db::getInstance()->rawQuery("SELECT product_id, product_name, product_order, " .
		"product_active, product_category FROM product" .
		$filterId . " ORDER BY product_category, product_order");

	$refPage->assign('tableSummary', _('Article list under use of the sedate filter') . ': ');
	$refPage->assign('productId', _('ID'));
	$refPage->assign('productName', _('Product-Name'));
	if (!empty($filterId))
		$refPage->assign('productOrder', _('Order'));
	else
		$refPage->assign('productOrder', ' ');
	$refPage->assign('productActiv', _('Activ'));
	$refPage->assign('productCategory', _('Category-<br>Name'));

	if ($products !== NULL)
	{
		$counter = 0;
		// Set datarows to display
		if (!isset($_SESSION['pageA']))
			$_SESSION['pageA'] = 1;
		if (ceil(count($products) / ARTICLE_LIST) < $_SESSION['pageA'])
			$_SESSION['pageA'] --;
		if ($_SESSION['pageA'] <= 0)
			$_SESSION['pageA'] = 1;
		$pageFirst = ($_SESSION['pageA'] * ARTICLE_LIST) - ARTICLE_LIST + 1;
		$pageLast = $_SESSION['pageA'] * ARTICLE_LIST;
		getPagination($refPage, count($products));
		$topOrderId = 0;
		$filterId = _setFilter($refValidate);

		foreach ($products as $product)
		{
			$counter ++;

			if ($counter < $pageFirst)
				continue;

			if ($counter > $pageLast)
				break;

			$product_namex = '<a href="index2.php?page=' . $page . '&sub=' . $subpage .
				'&id=' . $product['product_id'] .
				'&func=edit">' . $product['product_name'] . '</a>';

			if (!empty($filterId))
			{
				if ($product['product_order'] === 1)
				{
					$product_orderx = '<input type="text" name="orderm[]" id="orderm" value="' .
						$product['product_order'] . '" size="4" />' .
						'<input type="hidden" name="orderId[]" id="orderId[]" value="' .
						$product['product_id'] . '" />' .
						'<img class="orderArrow" src="template/images/page.article/top1.gif">';
				}
				else
				{
					$product_orderx = '<input class="NFText NFTextT" type="text" ' .
						'name="orderm[]" id="orderm" value="' . $product['product_order'] .
						'" size="4" />' .
						'<a href="index2.php?page=' . $page . '&sub=' . $subpage . '&cat=' .
						$product['product_category'] . '&order=' . $product['product_id'] .
						'&topid=' . $topOrderId . '">' .
						'<input type="hidden" name="orderId[]" id="orderId[]" value="' .
						$product['product_id'] . '" />' .
						'<img class="orderArrow" src="template/images/page.article/arrowup1.gif" ' .
						'title="' . _('One Position up') . '"></a>';
				}
				$topOrderId = $product['product_id'];
			}
			else
				$product_orderx = '-';

			$product_activex = '<a href="index2.php?page=' . $page . '&sub=' . $subpage . '&id=' .
				$product['product_id'] . '&active=1">';

			if ($product['product_active'] === '0')
			{
				$product_activex .= '<img src="template/images/page.article/notactive.gif" title="' . _('Activate') . '"></a>';
			}
			else
			{
				$product_activex .= '<img src="template/images/page.article/isactive.gif" title="' . _('Deactivate') . '"></a>';
			}

			db::getInstance()->where('category_id', $product['product_category']);
			$products_cat = db::getInstance()->getOne("category");

			$product_categoryx = $products_cat['category_name'];

			$productListx[] = array('productCheckboxx' => '<input type="checkbox" name="marker[]" ' .
			 ' value="' . $product['product_id'] . '" />',
			 'productIdx' => $product['product_id'],
			 'productNamex' => $product_namex,
			 'productOrderx' => $product_orderx,
			 'productActivx' => $product_activex,
			 'productCategoryx' => $product_categoryx);
		}
	}
	$refPage->assign('productListx', $productListx);

	if (!empty($filterId))
		$refPage->assign('btnOrder',
			'<input type="submit" name="submit" id="order" value="' . _('Save order') . '" />');
	else
		$refPage->assign('btnOrder', '');

	$refPage->assign('btnDelete',
		'<input type="submit" name="submit" id="delete" value="' . _('Delete') . '" />');

	$refPage->assign('btnNew',
		'<input type="submit" name="submit" id="new" value="' . _('New') . '" />');

	$refPage->assign('btnCopy',
		'<input type="submit" name="submit" id="copy" value="' . _('Copy') . '" />');

	$refPage->addtpl('content', '/' . $page . '/' . $subpage . '.tpl.php');
}
